package sjk;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class ImageDownloader {

    public static void downloadImage(String imageUrl, String savePath) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            try (InputStream inputStream = connection.getInputStream();
                 FileOutputStream outputStream = new FileOutputStream(savePath)) {

                byte[] buffer = new byte[1024];
                int bytesRead;

                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }

                System.out.println("图片下载成功，保存路径：" + savePath);
            }
        } catch (IOException e) {
            System.err.println("下载失败：" + e.getMessage());
        }
    }

    public static void main(String[] args) {
        String savePath = "E:/tmp/image.jpg"; // 替换为你的保存路径
        String imageUrl =  "https://gd-hbimg.huaban.com/6deaa9e22e29b8a92ad5b19babcc179020889d7b50a5dc-o6PQ6g"; // 替换为你的图片URL

        downloadImage(imageUrl, savePath);
    }
}
